package com.zkingsoft.web.interceptor;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysCompany;
import com.zkingsoft.web.bean.BusParameterSettings;
import com.zkingsoft.web.bean.WebNav;
import com.zkingsoft.web.service.BusParameterSettingsService;
import com.zkingsoft.web.service.WebNavService;
import com.zkingsoft.web.tool.SystemConstance;

/**
 * 通用web请求拦截器
 * 
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date 2017年11月29日
 */
public class HostRedirectInterceptor implements HandlerInterceptor {

	@Resource(name = "busParameterSettingsService")
	private BusParameterSettingsService busParameterSettingsService;

	@Resource(name = "webNavService")
	private WebNavService webNavService;

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	/**
	 * 通用拦截器
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		// 获得请求的域名
		String host = WebUtil.getRequest().getServerName();
		
		//判断是否为debug模式
		if(SystemConstance.DEBUG){
			String paramHost=request.getParameter("host");
			if(StringUtils.isNotBlank(paramHost)){
				host=paramHost;
				WebUtil.getSession().removeAttribute("company");
				LogUtil.debug("当前是调试模式");
			}
		}
		LogUtil.debug("当前请求域名{}", host);
		SysCompany company= WebUtil.getSessionAttribute("company");
		
		if (company != null) {
			LogUtil.debug("匹配到公司{}", company.getComName());
			return true;
		} else {
			
			@SuppressWarnings("unchecked")
			Map<String, SysCompany> companyMap = (Map<String, SysCompany>) WebUtil.getServletContext()
					.getAttribute("companyMap");
			Set<String> hostSet = companyMap.keySet();
			for (String key : hostSet) {
				LogUtil.debug("-- 匹配公司key={}，host={} 匹配结果={}", key , host, host.contains(key));
				if (host.contains(key)) {
					// 查到公司后存到sesssion中
					WebUtil.setSessionAttribute("company", companyMap.get(key));

					initCompanyBaseMsg(companyMap.get(key).getComId());

					return true;
				}
			}
		}
		LogUtil.debug("没有匹配到对应的公司");
		return false;
	}

	/**
	 * 初始化公司网站基本信息
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年11月15日
	 * @param comId
	 */
	private void initCompanyBaseMsg(String companyId) {
		BusParameterSettings query = new BusParameterSettings();
		query.setParamValue1(SystemConstance.CMSPZ);
		query.setCompanyId(companyId);
		List<BusParameterSettings> list = busParameterSettingsService.findByModel(query);
		for (BusParameterSettings param : list) {
			WebUtil.setSessionAttribute(param.getParamValue2(), param.getParamValue());
		}
		// 初始化导航
		WebNav webNav = new WebNav();
		webNav.setNavType("mobile");
		webNav.setCompanyId(companyId);
		webNav.setParentId(0L);
		webNav.setNavShow(WebNav.SHOW);
		WebUtil.setSessionAttribute("mobileNav", webNavService.findByModel(webNav));
		webNav.setNavType("pc");
		WebUtil.setSessionAttribute("pcNav", webNavService.findByModel(webNav));
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {

	 
	}

}
